from dbc_class import DBC,Message,Signal
import sys,re,os

# User Config Area
dbcfile = 'EVC4_AD_CANFD_ADAS_20240711_Draft.dbc'  # Default value unless overriden
self_node = 'ADAS'  # Default value unless overriden
FaceTo_node = 'Veh' # 'Veh'
Project_Name = 'Test'

def main(argv):
    dbcpath = os.path.dirname(os.path.abspath(__file__)) + '\\input\\' + dbcfile
    dbc = DBC(dbcfile, self_node,True,FaceTo_node)
    file = open(dbcpath,"r",encoding='ISO-8859-1')
    dbc.messages:dict[int,Message] = dbc.messages

    while 1:
        line = file.readline()
        if not line: # read over exit while breakpoint this you can see all class dbc 
            break
        
        # Nodes in the DBC file
        if line.startswith("BU_:"):
            nodes = line.strip("\n").split(' ')
            dbc.nodes = (nodes[1:])
            #FuncLib.ErrorPrint(self_node,dbc.nodes)
        
        # message in the DBC file  e.g.   BO_ 279 EMS_EngSt1: 8 GW
        if line.startswith("BO_ "):
            BO_line = line.split(' ')
            Msg_id = BO_line[1]
            Msg_name = BO_line[2].strip(':')
            Msg_length = BO_line[3]
            dbc.messages[Msg_id] = Message(Msg_id,Msg_name,BO_line[3],BO_line[4].strip('\n'))
            Current_Msgid = Msg_id
            Msg_id_Array = []
            
            # judge msg id lenth bounds
            dbc.messages[Msg_id].Bounds_Judge(Msg_id,Msg_length,Msg_id_Array)


        # singal in the DBC file  e.g.   SG_ EngInfo : 3|4@0+ (1,0) [0|15] ""  IDDC
        if line.startswith(" SG_ "):
            SG_line = line[1:].split()

            # if CheckSum_Str in line:
            #     dbc.messages[Current_Msgid].CheckSum = True
            # if 'ChkSum' in line:
            #     dbc.messages[Current_Msgid].CheckSum = True
            # if 'CheckSum' in line:
            #     dbc.messages[Current_Msgid].CheckSum = True
            # if 'CRC' in line:
            #     dbc.messages[Current_Msgid].CheckSum = True
            if ':' == SG_line[3] and 'm' in SG_line[2] :
                MultSig = 1
            else:
                MultSig = 0
            StartBit = re.split('[|@]', SG_line[3+MultSig])[0]
            BitSize = re.split('[|@]', SG_line[3+MultSig])[1]
            ByteOrder_ValueType = re.split('[|@]', SG_line[3+MultSig])[2]
            Factor = SG_line[4+MultSig][1:-1].split(',')[0]
            Offset = SG_line[4+MultSig][1:-1].split(',')[1]
            Min_Value = SG_line[5+MultSig][1:-1].split('|')[0]
            Max_Value = SG_line[5+MultSig][1:-1].split('|')[1]
            if '-' in SG_line[3+MultSig]: # signed number
                Signal_Min = (-(float(Factor) * pow(2,int(BitSize))) / 2) + float(Offset)
                Signal_Max = ((float(Factor) * pow(2,int(BitSize))) / 2) + float(Offset) - float(Factor)
            else:
                Signal_Min = 0 + float(Offset)
                Signal_Max = (float(Factor) * pow(2,int(BitSize))) + float(Offset) - float(Factor)
            Unit = SG_line[6+MultSig].strip('/""/') 
            Receiver = SG_line[7+MultSig].strip('\n').split(',')
            signals = Signal(SG_line[1],StartBit,BitSize,ByteOrder_ValueType,Factor,Offset,Min_Value,Max_Value,Unit,Receiver,Signal_Min,Signal_Max)
            dbc.messages[Current_Msgid].add_signal(signals)






if __name__ == "__main__":
    main(sys.argv[1:])